package com.bigbigrain.jiagou.design.command;

import com.bigbigrain.jiagou.design.command.impl.NoCommand;

/**
 * @description: 远程调度
 * @author: B1gB1gRA1n
 * @create: 2020-6-1 09:34
 **/
public class RemoteController {

    /**
     * 开按钮的命令组合
     */
    private Command[] onCommands;

    /**
     * 关按钮的命令组合
     */
    private Command[] offCommands;

    /**
     * 执行撤销命令
     */
    private Command undoCommand;

    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5 ; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    /**
     * 给按钮设置需要的命令
     * @param no 编号
     * @param onCommand 打开命令
     * @param offCommand 取消命令
     */
    public void setCommand(int no, Command onCommand, Command offCommand){
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 按下按钮
     * @param no 按钮编号
     */
    public void onButtonWasPushed(int no){
        //找到按下的开的按钮，并调用对应的方法
        onCommands[no].execute();
        //记录这次的操作，用户撤销
        undoCommand = onCommands[no];
    }

    /**
     * 按下关的按钮
     * @param no
     */
    public void offButtonWasPushed(int no){
        //找到按下的开的按钮，并调用对应的方法
        offCommands[no].execute();
        //记录这次的操作，用户撤销
        undoCommand = offCommands[no];
    }

    /**
     * 按下撤销按钮
     */
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}
